package com.JZ.QM.controller;

import com.JZ.QM.common.entity.PositionDetailEntity;
import com.JZ.QM.common.utils.CommonResult;
import com.JZ.QM.feign.UserInfoFeignService;
import com.JZ.QM.service.WealthAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedList;

/**
 * @author: cyc
 * @email cyc@gmail.com
 * @date: 2020/7/11 21:20
 */
@RestController
@RefreshScope
@Slf4j
@RequestMapping(value="/wealthAccount")
public class WealthAccountController {

    @Resource
    UserInfoFeignService userInfoFeignService;
    @Resource
    WealthAccountService wealthAccountService;

    /**
     * 显示持仓列表信息
     * @param userId 用户id
     * @param wealthAccountId 财富账号id
     * @return
     */
    @RequestMapping("/showWealthAccountList")
    public CommonResult showWealthAccount(Long userId,Long wealthAccountId){
        //用户的账号信息
        LinkedHashMap userAccountInfo= new LinkedHashMap();
        ArrayList positionList = new ArrayList();

        //获取账号信息
        userAccountInfo= (LinkedHashMap) userInfoFeignService.getUserAccountInfo(userId).get("userAccountInfo");
        if(userAccountInfo == null || userAccountInfo.isEmpty()){
            return CommonResult.error(201,"该用户不存在");
        } else {
            positionList=wealthAccountService.getPositionDetailList(Long.valueOf(userAccountInfo.get("accountId").toString()),wealthAccountId);
            return CommonResult.success().put("positionList",positionList);
        }

    }

    /**
     * 获取用户的累计持仓信息
     * @param userId
     * @return
     */
    @RequestMapping("/showTotalPosition")
    public CommonResult showTotalPosition(Long userId){
        //用户的账号信息
        LinkedHashMap userAccountInfo= new LinkedHashMap();
        //持仓基本信息
        PositionDetailEntity positionDetailEntity=new PositionDetailEntity();

        //获取账号信息
        userAccountInfo= (LinkedHashMap) userInfoFeignService.getUserAccountInfo(userId).get("userAccountInfo");
        if(userAccountInfo == null || userAccountInfo.isEmpty()){
            return CommonResult.error(201,"该用户不存在");
        } else {
            System.out.println("用户账号为"+userAccountInfo);
            positionDetailEntity=wealthAccountService.getTotalPositionDetail(userId,Long.valueOf(userAccountInfo.get("accountId").toString()));
            return CommonResult.success().put("positionDetail",positionDetailEntity);
        }

    }
}
